Date.prototype.format = function (format) {
	var o = {
		"M+": this.getMonth() + 1, //month 
		"d+": this.getDate(), //day 
		"h+": this.getHours(), //hour 
		"m+": this.getMinutes(), //minute 
		"s+": this.getSeconds(), //second 
		"q+": Math.floor((this.getMonth() + 3) / 3), //quarter 
		"S": this.getMilliseconds() //millisecond 
	};

	if (/(y+)/.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	}

	for (var k in o) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
		}
	}
	return format;
};

function findTextByValueInJson(jsonData, value) {
	var text = value;
	$.each(jsonData, function (entryIndex, entry) {
		if (entry['value'] == value) {
			text = entry['text'];
			return;
		}
	});
	return text;
}

/**
* 动态电子钟效果
* @returns {String}
*/
/*-----------------------------------------------------------------------------------
农历新年
*/
var CalendarData = new Array(100);
var madd = new Array(12);
var tgString = "甲乙丙丁戊己庚辛壬癸";
var dzString = "子丑寅卯辰巳午未申酉戌亥";
var numString = "一二三四五六七八九十";
var monString = "正二三四五六七八九十冬腊";
var weekString = "日一二三四五六";
var sx = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
var cYear, cMonth, cDay, TheDate;
CalendarData = new Array(0xA4B, 0x5164B, 0x6A5, 0x6D4, 0x415B5, 0x2B6, 0x957, 0x2092F, 0x497, 0x60C96, 0xD4A, 0xEA5, 0x50DA9, 0x5AD, 0x2B6, 0x3126E, 0x92E, 0x7192D, 0xC95, 0xD4A, 0x61B4A, 0xB55, 0x56A, 0x4155B, 0x25D, 0x92D, 0x2192B, 0xA95, 0x71695, 0x6CA, 0xB55, 0x50AB5, 0x4DA, 0xA5B, 0x30A57, 0x52B, 0x8152A, 0xE95, 0x6AA, 0x615AA, 0xAB5, 0x4B6, 0x414AE, 0xA57, 0x526, 0x31D26, 0xD95, 0x70B55, 0x56A, 0x96D, 0x5095D, 0x4AD, 0xA4D, 0x41A4D, 0xD25, 0x81AA5, 0xB54, 0xB6A, 0x612DA, 0x95B, 0x49B, 0x41497, 0xA4B, 0xA164B, 0x6A5, 0x6D4, 0x615B4, 0xAB6, 0x957, 0x5092F, 0x497, 0x64B, 0x30D4A, 0xEA5, 0x80D65, 0x5AC, 0xAB6, 0x5126D, 0x92E, 0xC96, 0x41A95, 0xD4A, 0xDA5, 0x20B55, 0x56A, 0x7155B, 0x25D, 0x92D, 0x5192B, 0xA95, 0xB4A, 0x416AA, 0xAD5, 0x90AB5, 0x4BA, 0xA5B, 0x60A57, 0x52B, 0xA93, 0x40E95);
madd[0] = 0;
madd[1] = 31;
madd[2] = 59;
madd[3] = 90;
madd[4] = 120;
madd[5] = 151;
madd[6] = 181;
madd[7] = 212;
madd[8] = 243;
madd[9] = 273;
madd[10] = 304;
madd[11] = 334;

function GetBit(m, n) {
	return (m >> n) & 1;
}
function e2c() {
	TheDate = (arguments.length != 3) ? new Date() : new Date(arguments[0], arguments[1], arguments[2]);
	var total, m, n, k;
	var isEnd = false;
	var tmp = TheDate.getYear();
	if (tmp < 1900) {
		tmp += 1900;
	}
	total = (tmp - 1921) * 365 + Math.floor((tmp - 1921) / 4) + madd[TheDate.getMonth()] + TheDate.getDate() - 38;

	if (TheDate.getYear() % 4 == 0 && TheDate.getMonth() > 1) {
		total++;
	}
	for (m = 0; ; m++) {
		k = (CalendarData[m] < 0xfff) ? 11 : 12;
		for (n = k; n >= 0; n--) {
			if (total <= 29 + GetBit(CalendarData[m], n)) {
				isEnd = true; break;
			}
			total = total - 29 - GetBit(CalendarData[m], n);
		}
		if (isEnd) break;
	}
	cYear = 1921 + m;
	cMonth = k - n + 1;
	cDay = total;
	if (k == 12) {
		if (cMonth == Math.floor(CalendarData[m] / 0x10000) + 1) {
			cMonth = 1 - cMonth;
		}
		if (cMonth > Math.floor(CalendarData[m] / 0x10000) + 1) {
			cMonth--;
		}
	}
}

function GetcDateString() {
	var tmp = "";
	//    tmp += tgString.charAt((cYear - 4) % 10);
	//    tmp += dzString.charAt((cYear - 4) % 12);
	//tmp += "";
	tmp += sx.charAt((cYear - 4) % 12);
	tmp += "年 ";
	if (cMonth < 1) {
		tmp += "(闰)";
		tmp += monString.charAt(-cMonth - 1);
	} else {
		tmp += monString.charAt(cMonth - 1);
	}
	tmp += "月";
	tmp += (cDay < 11) ? "初" : ((cDay < 20) ? "十" : ((cDay < 30) ? "廿" : "三十"));
	if (cDay % 10 != 0 || cDay == 10) {
		tmp += numString.charAt((cDay - 1) % 10);
	}
	return tmp;
}

function GetLunarDay(solarYear, solarMonth, solarDay) {
	//solarYear = solarYear<1900?(1900+solarYear):solarYear;
	if (solarYear < 1921 || solarYear > 2020) {
		return "";
	} else {
		solarMonth = (parseInt(solarMonth) > 0) ? (solarMonth - 1) : 11;
		e2c(solarYear, solarMonth, solarDay);
		return GetcDateString();
	}
}
//-----------------------------------------------------------------------------------------------

function getDateWeek() {
	todayDate = new Date();
	date = todayDate.getDate();
	month = todayDate.getMonth() + 1;
	year = todayDate.getYear();
	var dateweek = " ";
	if (navigator.appName == "Netscape") {
		dateweek = dateweek + (1900 + year) + "年" + month + "月" + date + "日";
	}
	if (navigator.appVersion.indexOf("MSIE") != -1) {
		dateweek = dateweek + year + "年" + month + "月" + date + "日";
	}
	//alert(GetLunarDay(year, month, date));

	var yy = todayDate.getFullYear();
	if (yy < 100) yy = "19" + yy;
	dateweek += "(" + GetLunarDay(yy, todayDate.getMonth() + 1, todayDate.getDate()) + ") ";

	switch (todayDate.getDay()) {
		case 0: dateweek += "星期日"; break;
		case 1: dateweek += "星期一"; break;
		case 2: dateweek += "星期二"; break;
		case 3: dateweek += "星期三"; break;
		case 4: dateweek += "星期四"; break;
		case 5: dateweek += "星期五"; break;
		case 6: dateweek += "星期六"; break;
	}
	return dateweek;
}

var itemShowTime = null;
function getDispItem() {
	if (itemShowTime == null) {
		itemShowTime = document.getElementById('lblShowTime');
	}
	return itemShowTime;
}

function showClock() {
	setInterval(function () {
		var d = new Date();
		hours = d.getHours();
		minutes = d.getMinutes();
		seconds = d.getSeconds();
		minutes = (minutes < 10) ? "0" + minutes : minutes;
		seconds = (seconds < 10) ? "0" + seconds : seconds;
		time = " " + hours + ":" + minutes + ":" + seconds;
		time = getDateWeek() + time + "&nbsp;&nbsp;";
		var showDiv = getDispItem();
		if (showDiv != null) {
			showDiv.innerHTML = time;
		}
	}, 1000);
}


//生产随机数
function uuid32() {
	var s = [];
	var h = new Date().getTime() + "abc"
	//var h = "0123456789abcdef";

	for (var i = 0; i < 32; i++) {
		s[i] = h.substr(Math.floor(Math.random() * 0x10), 1);
	}
	s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
	s[19] = h.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
	return s.join("");
}
//生产随机数
function uuid13() {
	return Date.parse(new Date());
}

/**
 * 校验内容  是否为 "" 、 空格 、 或 值为null,或者换行符
 * @param str
 * @returns
 */
function isNull(str) {
	if (str == null) return true;
	str = $.trim(str);
	if (str == "") return true;
	if (str == "null") return true;
	if (str == undefined) return true;
}

/**
 * 时间格式化
 */
Date.prototype.format = function (fmt) {
	var o = {
		"M+": this.getMonth() + 1, //月份 
		"d+": this.getDate(), //日 
		"h+": this.getHours(), //小时 
		"m+": this.getMinutes(), //分 
		"s+": this.getSeconds(), //秒 
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度 
		"S": this.getMilliseconds() //毫秒 
	};
	if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

/**
 * 拼接sql元素
 * parameter objData js对象
 * return string 
 */
function appendSqlEle(objData) {
	delete objData.id;
	var attr = Object.keys(objData)
	var sql = "";
	var id = "";
	for (var i = 0; i < attr.length; i++) {
		var key = attr[i];
		if (i < attr.length - 1) {
			if (key != "id") {
				sql += key + "=?,";
			}
		} else {
			if (key != "id") {
				sql += key + "=?";
			}
		}
	}
	return sql;
}
/*function appendSqlEle(objData){
	delete objData.id;
	var dataJson = JSON.stringify(objData)
	dataJson = dataJson.substring(dataJson.indexOf("{")+2,dataJson.lastIndexOf("}"));
	var json = dataJson.split(",");
	var sql = "";
    for(var i=0;i<json.length;i++){
    	var map = json[i].split(":");
    	var key = map[0].replace(/"/g, '');
    	var value = map[1].replace(/"/g, '');
    	if (i<json.length-1){
    		sql+=key+"=?,";
    	} else {
    		sql+=key+"=?";
    	}
    }
    return sql;
} */

/**
 * 处理批量保存数据
 * parameter objData js对象
 * return [[],[]]
 */
function getArrayParam(arrayObjData) {
	var arrayParam = [];
	for (var j = 0; j < arrayObjData.length; j++) {
		delete arrayObjData[j].$$hashKey;
		var attr = Object.keys(arrayObjData[j])
		var sql = "";
		var data = [];
		var id = "";
		for (var i = 0; i < attr.length; i++) {
			var key = attr[i];
			var value = arrayObjData[j][key];
			if (key != "id") {
				data.push(value);
			} else {
				id = value;
			}

			if (i == attr.length - 1) {
				data.push(id);
			}
		}
		arrayParam.push(data);
	}
	return arrayParam;
}
/*
function getArrayParam(arrayObjData){
	var arrayParam = [];
	for(var j=0;j<arrayObjData.length;j++){
		delete arrayObjData[j].$$hashKey;
	  	var dataJson = JSON.stringify(arrayObjData[j])
    	dataJson = dataJson.substring(dataJson.indexOf("{")+2,dataJson.lastIndexOf("}"));
		var json = dataJson.split(",");
    	var sql = "";
    	var data = [];
    	var id = "";
	    for(var i=0;i<json.length;i++){
	    	var map = json[i].split(":");
	    	var key = map[0].replace(/"/g, '');
	    	var value = map[1].replace(/"/g, '')=="null"?"":map[1].replace(/"/g, '');
	    	if (key != "id"){
    			data.push(value);
    		} else {
    			id = value;
    		}
	    	
	    	if (i == json.length-1){
	    		data.push(id);
	    	}
	    }
	    arrayParam.push(data);
	}
    return arrayParam;
}*/
/**
 * 金额显示0.00格式的处理
 * @param value
 * @returns
 */
function processMoney(value) {
	value = Math.round(parseFloat(value) * 100) / 100;
	var xsd = value.toString().split(".");
	if (xsd.length == 1) {
		value = value.toString() + ".00";
		return value;
	}
	if (xsd.length > 1) {
		if (xsd[1].length < 2) {
			value = value.toString() + "0";
		}
		return value;
	}
}

/**
 * 生成表单编号
 * @param regularCode regularCode 编号规则码 例：CGD
 * @param length      所需序列号长度
 * @returns {String}
 */
function getFormCode(regularCode, length, callBack) {
	if (isNull(regularCode)) {
		alert("regularCode can not be null");
		return false;
	}
	if (isNull(length)) {
		alert("length can not be null");
		return false;
	}
	generateCode(regularCode, length, 1, function (d) {
		getSQ(length, regularCode, d, function (formCode) {
			callBack(formCode);
		});
	});
}

/**
 * 
 * @param regularCode  编号规则码 例：CGD
 * @param length       所需序列号长度
 * @param tryTimes     尝试生成编号次数，默认一次
 * @returns {String}
 */
function generateCode(regularCode, length, tryTimes, callBack) {
	try {
		var nowDate = new Date().format("yyyyMMdd");
		db.selectBysql("select * from t_serial_number where regularCode = ? and createDate = ? order by numberx desc", [regularCode, nowDate],
			function (d) {
				var data = {};
				console.log(d);
				if (d.length < 1) {
					data.id = uuid32();
					data.createDate = new Date().format("yyyyMMdd");
					data.numberx = 1;
					data.regularCode = regularCode;
					db.insertBysql("t_serial_number", data, function (d) { });
					callBack(data.numberx);
				} else {
					var num = d[0].numberx + 1
					var createDate = new Date().format("yyyyMMdd");
					var sql = "update t_serial_number set createDate = ? , numberx = ? ,regularCode = ? where id = ?";
					var data = [createDate, num, regularCode, d[0].id];
					db.updateBysql(sql, data, function (d) { });
					callBack(num);
				}
			});
	} catch (e) {
		tryTimes++;
		if (tryTimes < 5) {
			generateCode(regularCode, length, tryTimes);
		}
	}
}

/**
 * 拼接编号
 * @param length 所需序列号长度
 * @param regularCode 编号规则码 例：CGD
 * @param number 序列号
 * @returns
 */
function getSQ(length, regularCode, number, callback) {
	var x = 0;
	x = number;
	var str = regularCode;
	var xx = x + "";
	var d = "";
	for (var i = 1; i <= length - xx.length; i++) {
		d += "0";
	}
	var nowDate = new Date().format("yyyyMMdd");
	str += nowDate;
	d += x;
	str += d;
	callback(str);
}
try {
	$(function () {

		$("#reset").click(function () {
			$("#formQuery  input[type='text']").val("");
			$("#formQuery  input[type='hidden']").val("");
			$("#formQuery  select").val("");
			$("#formQuery  textarea").val("");
			$("#formQuery  :checkbox").removeAttr("checked");
			$("#formQuery  :radio").removeAttr("checked");
		});


		//			$("input").bind("input propertychange",function(){
		//				var val = $(this).val();
		//				val = $.trim(val);
		//				$(this).val(val)
		//			});
	});
} catch (e) {
}
function notDisabled(id, isShow) {
	if (isShow) {
		$("#" + id).removeClass("color-button");
		$("#" + id).attr("disabled", isShow);

	} else {
		$("#" + id).addClass("color-button");
		$("#" + id).attr("disabled", isShow);
	}
}
function handLike(str) {
	var pattern = new RegExp("[?\\']");
	var rs = "";
	for (var i = 0; i < str.length; i++) {
		rs = rs + str.substr(i, 1).replace(pattern, '');
	}
	return rs;
}

//去空格
function Trim(str) {
	if (!isNull(str)) {
		return str.replace(/(^\s*)|(\s*$)/g, "");
	}
	return str;
}
//检验非空
function isNumber(val) {
	var regPos = /^\d+(\.\d+)?$/; //非负浮点数
	var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
	if (regPos.test(val) || regNeg.test(val)) {
		return true;
	} else {
		return false;
	}

}

//处理in

function arrayIn(array) {
	var str = "";
	for (var i = 0; i < array.length; i++) {
		var s = "'" + array[i] + "'";
		str = (str === "" ? s : str + "," + s);
	}
	return str;
}


//计算----
//浮点数加法运算  
function FloatAdd(arg1, arg2) {
	var r1, r2, m;
	try {
		r1 = arg1.toString().split(".")[1].length;
	} catch (e) {
		r1 = 0;
	}
	try {
		r2 = arg2.toString().split(".")[1].length;
	} catch (e) {
		r2 = 0;
	}
	m = Math.pow(10, Math.max(r1, r2));
	return (arg1 * m + arg2 * m) / m;
}

// 浮点数减法运算
function FloatSub(arg1, arg2) {
	var r1, r2, m, n;
	try {
		r1 = arg1.toString().split(".")[1].length;
	} catch (e) {
		r1 = 0;
	}
	try {
		r2 = arg2.toString().split(".")[1].length;
	} catch (e) {
		r2 = 0;
	}
	m = Math.pow(10, Math.max(r1, r2));
	// 动态控制精度长度
	n = (r1 >= r2) ? r1 : r2;
	return ((arg1 * m - arg2 * m) / m).toFixed(n);
}

// 浮点数乘法运算
function FloatMul(arg1, arg2) {
	var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
	try {
		m += s1.split(".")[1].length;
	} catch (e) {
	}
	try {
		m += s2.split(".")[1].length;
	} catch (e) {
	}
	return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}

// 浮点数除法运算
function FloatDiv(arg1, arg2) {
	var t1 = 0, t2 = 0, r1, r2;
	try {
		t1 = arg1.toString().split(".")[1].length;
	} catch (e) {
	}
	try {
		t2 = arg2.toString().split(".")[1].length;
	} catch (e) {
	}
	with (Math) {
		r1 = Number(arg1.toString().replace(".", ""));
		r2 = Number(arg2.toString().replace(".", ""));
		return (r1 / r2) * pow(10, t2 - t1);
	}
}

// 浮点数余数运算
function FloatResidue(arg1, arg2) {
	var t1 = 0, t2 = 0, r1, r2;
	try {
		t1 = arg1.toString().split(".")[1].length;
	} catch (e) {
	}
	try {
		t2 = arg2.toString().split(".")[1].length;
	} catch (e) {
	}
	with (Math) {
		r1 = Number(arg1.toString().replace(".", ""));
		r2 = Number(arg2.toString().replace(".", ""));

		return FloatMul((r1 % r2), pow(10, t2 - t1));
	}
}
//获取浮点型的后几位，
function FloatDigit(f, d) {
	if (f.indexOf(".") < 1) {
		return f;
	} else {
		var str = f.split(".");
		return str[0] + str[1].substr(0, d);
	}
}


//计算----
//读取文件，返回map形式
function readFileMap(fs, filepath) {
	//const fs =    window.parent.require("fs");
	var setting = fs.readFileSync(filepath).toString();
	var arrObj = setting.split(";");
	var obj = {};
	for (var i = 0; i < arrObj.length; i++) {
		if (!isNull(arrObj[i])) {
			var arr = arrObj[i].split("=");
			obj[arr[0].trim()] = arr[1].trim();
		}

	}
	return obj;
}

//读取文件，返回map形式
function readcustomsetting(db, success, fail) {
	db.selectBysql("select * from customsetting", [], function (data) {
		if (success) {
			if (data.length > 0) {
				//var o = d[0];
				var obj = {};
				for (var i = 0; i < data.length; i++) {
					obj[data[i].customkey] = data[i].customkeyvalue
				}

				success(obj);
			} else {
				fail("查询不出数据" + customkey)
			}
		}
	}, function (err) {
		if (err) {
			fail(err)
		}
	})
}

function updatecustomsetting(db, customkey, value, success, fail) {
	console.log(customkey + "===" + value);
	db.updateBysql("update customsetting set customkeyvalue=? where customkey=?", [value, customkey], function (data) {
		if (success) {
			success();
		}
	}, function (err) {
		if (err) {
			fail(err)
		}
	})
}

/***
 * 处理汉子转拼音问题
 */
window.pinyin = window.pinyin || {};
(function (py) {
	var req = window.require || window.top.require;
	var pinyin = req("pinyin");
	py.frist = function (hanzi) {
		var r = pinyin(hanzi, {
			style: pinyin.STYLE_FIRST_LETTER, // 设置拼音风格
			heteronym: true
		})
		if (r) {
			return r.join("").toUpperCase();
		} else {
			return "";
		}
		// return 
	}

})(window.pinyin)
window.pinyin = window.pinyin;